package com.gjy.concurrency.queue;

import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-03-08 14:26:21
 */
public class Queue1Test {

    @Test
    public void test4() throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(4);
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(2);
        for (int i = 0; i < 10; i++) {
            pool.execute(() -> {
                try {
                    blockingQueue.put(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        TimeUnit.SECONDS.sleep(1);
        System.out.println("blockingQueue = " + blockingQueue);
    }

    @Test
    public void test3() throws Exception {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(2);

        new Thread(() -> {
            try {
                blockingQueue.put(1);
                System.out.println("blockingQueue = " + blockingQueue);
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "A").start();

        new Thread(() -> {
            try {
                blockingQueue.put(1);
                System.out.println("blockingQueue = " + blockingQueue);
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "B").start();

        new Thread(() -> {
            try {
                blockingQueue.put(1);
                System.out.println("blockingQueue = " + blockingQueue);
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "C").start();
        new Thread(() -> {
            try {
                blockingQueue.put(1);
                System.out.println("blockingQueue = " + blockingQueue);
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "D").start();

        new Thread(() -> {
            System.out.println("Hello World");
            try {
                System.out.println("blockingQueue.take() = " + blockingQueue.take());
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "E").start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("blockingQueue = " + blockingQueue);
    }

    @Test
    public void test1() {
        BlockingDeque<Integer> bd = new LinkedBlockingDeque<>();
        bd.addFirst(1);
        bd.add(3);
        bd.addFirst(5);
        System.out.println("bd = " + bd);
    }

    @Test
    public void test2() {
        BlockingDeque<Integer> bd = new LinkedBlockingDeque<>(2);
        bd.offer(1);
        bd.offer(1);
        bd.offer(1);
        System.out.println("bd = " + bd);
    }
}
